package com.cn.wanxi.servlet.back.product;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.service.IProductService;
import com.cn.wanxi.service.impl.ProductServiceImpl;
import com.cn.wanxi.util.DataEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/product")
public class ProductServlet extends HttpServlet {
    IProductService productService = new ProductServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //产品列表
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        List<ProductModel> productList = productService.getProductList();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("products",productList);
        resp.getWriter().print(jsonObject);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //产品分页列表
        DataEncoding.setJsonContentType(resp);
        DataEncoding.setCharacterEncoding(req,resp);
        String pageSize = req.getParameter("page");
        String pageNum = req.getParameter("size");
        int page = Integer.parseInt(pageSize);
        int size = Integer.parseInt(pageNum);
        List<ProductModel> productList = productService.getProductList(page,size);
        long total = productService.getProductCount();
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("products",productList);
        jsonObject.put("total",total);
        resp.getWriter().print(jsonObject);
    }
}
